package process;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;


public class ProcessController {

	  private CPU cpu ;
	
//	单例
	static ProcessController singleton;
	static ProcessController getProcessController() {
		return singleton;
	}
//	单例
	
	
	@FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextArea instructionOutputArea;

    @FXML
    private Label showAX;

    @FXML
    private Rectangle showFlag;

    @FXML
    private Label showRelativeTime;

    @FXML
    private Label showSystemTime;
    
    @FXML
    private Label showPid;

    @FXML
    void initialize() {
        assert instructionOutputArea != null : "fx:id=\"instructionOutputArea\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        assert showAX != null : "fx:id=\"showAX\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        assert showFlag != null : "fx:id=\"showFlag\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        assert showRelativeTime != null : "fx:id=\"showRelativeTime\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        assert showSystemTime != null : "fx:id=\"showSystemTime\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        assert showPid != null : "fx:id=\"showPid\" was not injected: check your FXML file 'ProcessManagerFXML.fxml'.";
        
        singleton = this;
        cpu = CPU.getCpu();
        refresh();     			
        instructionOutputArea.promptTextProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				refresh();
			}
		});
    }

    
    void refresh() {
    	showAX.setText(Integer.toString(cpu.getAx())); 
    	showRelativeTime.setText(Integer.toString(cpu.getRelativeTime()));
    	showSystemTime.setText(Integer.toString(cpu.getSystemTime()));
    	showPid.setText(Integer.toString(cpu.getPcb().getPid()));
    }
    
    
    public TextArea getinstrurctionOutPutArea() {
    	return instructionOutputArea;
    }
    public void outPut(String str) {
    	Platform.runLater(() -> {
			instructionOutputArea.appendText(str);
		});
    	
    }
    private Color flagColor =Color.GREEN;
    public void setShowFlag() {
    	if(flagColor.equals(Color.RED))flagColor =Color.GREEN;
    	else flagColor =Color.RED;
    	showFlag.setFill(flagColor);
    }

}
